<?php
$a = '{"state":"fail","code":519,"msg":"\u6e38\u73a9\u65f6\u95f4\u672a\u5230","pckData":"4d21110088888888262000000401070200"}';
print_r(json_decode($a));exit;
/*下单http://www.12301.cc/meituan/MT_All_Order.php?mobile=15959110796&orderid=1443511528&outergoodsid=2115|7641|16666&num=2&method=send&item_title=%E8%93%9D%E8%B0%83%E5%BA%84%E5%9B%AD%E6%B8%A9%E6%B3%89%E9%97%A8%E7%A5%A8&consume_type=1&sms_template=&mms_template=&token=353250145660&timestamp=1402987039&valid_start=2015-09-29+14%3A37%3A19&valid_ends=1970-01-01+08%3A00%3A00&app_key=ea59253a4292cbff10e3bb1ba1db01a490aabcf0&sign=bcc53d2bc980f564d3cf34ef99e078f63484f315
*/
/*取消http://www.12301.cc/meituan/test.php?orderid=1443412912&method=cancel&verifycode=576207420720&timestamp=1401088833&app_key=ea59253a4292cbff10e3bb1ba1db01a490aabcf0&sign=90ab3f8d04f4914e4d7ca5e4338ae0fe0bc961e3*/
/*查询http://www.12301.cc/meituan/test.php?orderid=1900624552&method=query&verifycodes=926413&timestamp=2014-05-31+00%3A00%3A00&app_key=ea59253a4292cbff10e3bb1ba1db01a490aabcf0&sign=2b4a885d3d98e117dd043a62aea4044e9986e6ad*/

$data=json_encode($_REQUEST);
$ip = GetIP();

err($data."|".$ip,'get_data');
// err("------------------------".date('Y-m-d H:i:s')."------------------------------");

// $sign_key = '4866a0b329f143279a9ec80a8247b014818db49f'; 
$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$s1=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));

if (!class_exists("go_sql")){
include("/var/www/html/new/conf/le.je");
  $le=new go_sql();
  $le->connect();
}

$method = $_GET['method'];
$app_key = $_GET['app_key'];//唯一标识符

if($method=='send' && $app_key=='ea59253a4292cbff10e3bb1ba1db01a490aabcf0'){
	//这里是提交订单
	$phone =  $_GET['mobile']; //手机号码
	$orderid = $_GET['orderid']; //远端订单号
	$ids =  $_GET['outergoodsid'];//门票id
	$idss = explode('|',$ids);
	$landid = $idss[0]; //供应商id
	$lid = $idss[1]; //景区id
	$tid = $idss[2]; //门票id
	$num = $_GET['num'];//数量
	$playtime =strtotime($_GET['valid_start']); //预计游玩时间
	$playtime = date("Y-m-d",$playtime);
	$price = 100;
	
	$arr = array();
	for($i=1;$i<=$num;$i++)
	{
		$submit_array = array("ac"=>'200750',"pw"=>'6f3fbb18f77b34f5bc0a75e67b58af53',"in0"=>$lid,"in1"=>$tid,"in2"=>$orderid."_".$i,"in3"=>$price,"in4"=>1 ,"in5"=>$playtime,"in6"=>'美团',"in7"=>$phone,"in8"=>"","in9"=>1,"in10"=>2,"in11"=>0,"in12"=>"","in13"=>"","in14"=>0,"in15"=>0,"in16"=>$landid);
		$xml2=$s1->__soapCall("PFT_Order_Submit",$submit_array);
		$result=simplexml_load_string($xml2);
		if((string)$result->Rec->UUordernum){ 
			//下单成功赋值凭证码，或者出现1075远端订单号重复的情况
			$codesss=(string)$result->Rec->UUcode; 
			$arr[]=$codesss;
		} 
		else{	
			if((int)$result->Rec->UUerrorcode==122){
			//授信不足，提醒供应商给分销商授信
				$str="select mobile from pft_member where id='$landid' limit 1";			
				$GLOBALS['le']->query($str);
				$GLOBALS['le']->fetch_assoc();
				$aid_Tel=$GLOBALS['le']->f('mobile');
				if($aid_Tel){
					$s->Send_SMS_V($aid_Tel,"系统公告:您的分销商[【美团】北京三快在线科技有限公司]因授信不足下单失败，失败订单号:".$orderid.",请尽快补充授信后联系OTA商家补单，避免影响销量");
				}
			}
			err($orderid.'|'.(int)$result->Rec->UUerrorcode,'send_fail');
			break;
		}
	}
	if($arr){
		$tmp['code']=200;
		$tmp['err_str']='success';
		$tmp['verifycodes']=$arr;
		err(json_encode($tmp)."|".$orderid."|".$ip,'send_success');
		echo json_encode($tmp);
	}else{ 
	    $str ='{"code":"300"}';
		echo $str;
	}
	
}elseif($method=='cancel' && $app_key=='ea59253a4292cbff10e3bb1ba1db01a490aabcf0'){
	//这里是取消订单
	$code = $_GET['verifycode']; //获取凭证码
	if(strlen($code) != 6){//第三方优惠码
		$select = "select pftOrder,oStatus from all_api_order  where apiCode='$code' limit 1 ";
		$GLOBALS['le']->query($select);
		if(!$GLOBALS['le']->fetch_assoc()) exit('{"code":"300"}');
		$pt_ordernum = $GLOBALS['le']->f('pftOrder');
		$oStatus = $GLOBALS['le']->f('oStatus');
		if($oStatus != 0 ) exit('{"code":"300"}');
		$cancel = $s->Order_Change_Pro($pt_ordernum,0,-1,0,0,"","","");
		if($cancel == 100){
			//取消成功
			err($pt_ordernum."|200|".$ip,'cancel_success');
			exit('{"code":"200"}');	 
		}else{
			err($pt_ordernum.'|'.$orderid.'|'.$cancel.'|'.$ip,'cancel_fail');
			exit('{"code":"300"}');
		}		
	}
	$code = str_replace("，",",",$code);
	$orderid = $_GET['orderid']."_1";
	$result = search_lid($orderid,$code,$s,$s1,$orderid);
	if(!$result){
		err($_GET['verifycode'].'|'.$orderid.'|'.'未找到订单'.'|'.$ip,'cancel_fail');
		exit('{"code":"300"}');
	}
	foreach($result as $k){
		if(empty($k)) continue;
		$pt_ordernum = (int)$k->Rec->UUordernum;
		$cancel = $s->Order_Change_Pro($pt_ordernum,0,-1,0,0,"","","");
		if($cancel == 100){
			//取消成功
			err($pt_ordernum."|200|".$ip,'cancel_success');
			$str ='{"code":"200"}';
			echo $str;		 
		}else{
			err($pt_ordernum.'|'.$orderid.'|'.$cancel.'|'.$ip,'cancel_fail');
			$str ='{"code":"300"}';
			echo $str;
		}
	}
}
elseif($method=='query' && $app_key=='ea59253a4292cbff10e3bb1ba1db01a490aabcf0'){//这里是查询订单
	$code = $_GET['verifycodes'];
	$code = str_replace("，",",",$code);
	$select = "select pftOrder,oStatus,apiCode from all_api_order  where apiCode in($code) ";
	$GLOBALS['le']->query($select);	
	$api_data = array();
	$arr = array();
	while($row = $GLOBALS['le']->fetch_assoc())	$api_data[] = $row;
	if(count($api_data)){
		foreach($api_data as $k){
			if($k['oStatus']==0){
				$UUstatus = 0;
				$canceled_num = 0;
				$used_num = 0;
			}elseif($k['oStatus']==3){
				//取消
				$UUstatus = 2;
				$canceled_num = 1 ;
				$used_num = 0;
			}elseif($k['oStatus']==1){
				$UUstatus = 1;
				$used_num = 1;
				$canceled_num = 0;
			}
			$tmp['code']=$k['apiCode'];
			$tmp['status']=$UUstatus;
			$tmp['used_num'] = $used_num;
			$tmp['canceled_num'] = $canceled_num;
			$arr[] =$tmp;
		}		
	}	
	else{
		$orderid = $_GET['orderid']."_1";	
		$selOrderlid = search_New($orderid,$s);
		$lid = (int)$selOrderlid->Rec->UUlid;
		$UUremotenum = (string)$selOrderlid->Rec->UUremotenum;
		$UUsalerid = (string)$selOrderlid->Rec->UUsalerid;
		$codes  = explode(',',$code);
		foreach($codes as $k){
			$see_code = search_Code($UUsalerid,$k,$s);
			print_r($see_code);
		}
	}
}


function search_lid($orderid,$code,$s,$s1){
	// echo $orderid;
	$get_lid_date = $s1->__soapCall("Order_Globle_Search",array("ac"=>'200750',"pw"=>'6f3fbb18f77b34f5bc0a75e67b58af53',"in0"=>"","in1"=>"","in2"=>"","in3"=>"","in4"=>"","in5"=>"","in6"=>"","in7"=>"","in8"=>"","in9"=>"","in10"=>"","in11"=>"","in12"=>"","in13"=>$orderid,"in14"=>"","in15"=>"","in16"=>"","in17"=>"","in18"=>"","in19"=>"","in20"=>"","in21"=>1,"in23"=>"","in24"=>"","in25"=>"","in26"=>""));
	// print_r($get_lid_date);exit;
	if(strlen($get_lid_date)<=47) exit('{"code":"300","err_str":"fail"}'); //查无此订单 
	// echo 111;exit;
	$get_lid_date_xml =simplexml_load_string($get_lid_date);
	$UUp_type= (string)$get_lid_date_xml->Rec->UUp_type;
	if($UUp_type == 'F'){  //套票查询子票
		$mrOrders = explode("_",$orderid);
		$mrOrder = $mrOrders[0];
		$code_array = explode(',',$code);
		$count = count($code_array);
		for($i=1;$i<=$count;$i++){
			$mtOrder = $mrOrder.'_'.$i;
			$orders = Select_Childs($s,$mtOrder);
			$orderss[] = $orders;
		}
		if($orderss){
			$str ='{"code":"200","err_str":"success","result":'.json_encode($orderss).'}';
			echo $str;
		}
		else{
			$str ='{"code":"300","err_str":"fail"}';
			echo $str;
		}
		exit;
	}else{
		$lid= (int)$get_lid_date_xml->Rec->UUlid;
		print_r(array("1853",$code,-1,$lid));exit;
		$get_code =$s->PFT_Order_Code("1853",$code,-1,$lid);
		$result= simplexml_load_string($get_code);
		// print_r($result);exit;
		foreach($result->Rec as $orders){
				$selOrder = Select_PFTorder((string)$orders->UUordernum,$s,$orderid);	
				if($selOrder != 101) $selOrders[] = $selOrder;
		}
		return $selOrders;
	}
}
function err($txt,$path){
	$fp = fopen('/var/www/html/new/d/meituan/logs/'.$path.date('Y-m-d').'.txt',"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}

function Select_REMOorder($order,$s){
	$pft_order = $s->Order_Globle_Search(
			0,0,0,0,"",
			"","","","","",
			"","","","",0,
			"","","",1,0,
			0,1,0,"","",
			"",$order,"","","",
			"","","","",""
	);
	$pft_order_xml =simplexml_load_string($pft_order);
	return $pft_order_xml;
}
function Select_PFTorder($order,$s,$orderid){
	$pft_order = $s->Order_Globle_Search(
		0,0,0,0,"",
		"","","","","",
		"","",$order,"",0,
		"","","",1,0,
		0,1,"","","",
		"","","","","",
		"","",1,"",""
	);
	$pft_order_xml =simplexml_load_string($pft_order);
	$UUremotenums = explode("_",(string)$pft_order_xml->Rec->UUremotenum);
	$UUremotenum = $UUremotenums[0];	
	$mrOrders = explode("_",$orderid);
	$mrOrder = $mrOrders[0];
	if($UUremotenum == $mrOrder)
		return $pft_order_xml;
	else{
		err($UUremotenum.'|'.$mrOrder.'|订单号错误','query_response');
		return 101;
	}
}
function search_New($orderid,$s){
	$pft_order = $s->Order_Globle_Search(
		0,0,0,0,"",
		"","","","","",
		"","","","",0,
		"","","",1,0,
		0,1,0,"","",
		"",$orderid,"","","",
		"","","","",""
	);
	$pft_order_xml =simplexml_load_string($pft_order);
	return $pft_order_xml;
}
function search_Code($salerID,$Code,$s){
	print_r(array($salerID,$Code,""));exit;
	$see_code = $s->Code_Verify_Order($salerID,$Code,"");
	return $see_code;
}
function Select_Childs($s,$orderid){
	$pft_order = $s->Order_Globle_Search(
			0,0,0,0,"",
			"","","","","",
			"","","","",0,
			"","","",1,0,
			0,1,0,"","",
			"",$orderid,"","","",
			"","","","",""
	);
	$pft_order_xml =simplexml_load_string($pft_order);
	$UUordernum= (string)$pft_order_xml->Rec->UUordernum;
	$xmlChildOrders = $s->Order_Globle_Search(
			"","","","","",
			"","","","","",
			"","",$UUordernum,"","",
			"","","",1,1,
			0,999,"","","",
			"","","","","",
			"",0,2
		);
	$simxml2 = simplexml_load_string($xmlChildOrders);
	foreach($simxml2 as $orders){
		if((int)$orders->UUstatus == 1){
			// 已使用或者部分使用
			$UUstatus = 1;
			$used_num = 1;
			$canceled_num = 0;
			break;
		}elseif((int)$orders->UUstatus == 3){
			// 取消
			$UUstatus = 2;
			$canceled_num = 1 ;
			$used_num = 0;
			break;
		}elseif((int)$orders->UUstatus == 0 || (int)$orders->UUstatus==2){
			// 未使用或者已过期
			$UUstatus = 0;
			$canceled_num = 0;
			$used_num = 0;
		}			
	}
	$tmp['code']=(string)$pft_order_xml->Rec->UUcode;
	$tmp['status']=$UUstatus;
	$tmp['used_num'] = $used_num;
	$tmp['canceled_num'] = $canceled_num;
	
	return $tmp;
}
	
function GetIP(){
	if(!empty($_SERVER["HTTP_CLIENT_IP"])){
		$cip = $_SERVER["HTTP_CLIENT_IP"];
	}elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
		$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}elseif(!empty($_SERVER["REMOTE_ADDR"])){
		$cip = $_SERVER["REMOTE_ADDR"];
	}
	else{
	  $cip = "无法获取！";
	}
	return $cip;
}

?> 